package com.lapelife.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lapelife.backend.common.ResponseResult;
import com.lapelife.backend.model.request.PageRequest;
import com.lapelife.backend.service.PostUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "文章操作管理")
@RestController
@RequestMapping("post/user")
public class PostUserController {
    private PostUserService postUserService;

    @Autowired
    public void setPostService(PostUserService postUserService) {
        this.postUserService = postUserService;
    }

    /**
     * 文章点赞
     *
     * @param flag 0：不点赞，1：点赞
     * @param id   文章id
     * @return
     */
    @ApiOperation("文章点赞")
    @GetMapping("/thumbs/{flag}/{id}")
    @PreAuthorize("hasAuthority('post:user:thumbs')")
    public ResponseResult<String> thumbs(@PathVariable String flag, @PathVariable Long id) {
        return postUserService.thumbs(flag, id);
    }

    /**
     * 文章收藏
     *
     * @param flag 0：不收藏，1：收藏
     * @param id   文章id
     * @return
     */
    @ApiOperation("文章收藏")
    @GetMapping("/collect/{flag}/{id}")
    @PreAuthorize("hasAuthority('post:user:collect')")
    public ResponseResult<String> collect(@PathVariable String flag, @PathVariable Long id) {
        return postUserService.collect(flag, id);
    }

    /**
     * 文章阅读，用户点击文章详情后，自动调用该接口
     * 文章历史记录，记录用户访问过的文章
     *
     * @param id 文章id
     * @return
     */
    @ApiOperation("文章阅读")
    @GetMapping("/look/{id}")
    @PreAuthorize("hasAuthority('post:user:look')")
    public ResponseResult<String> look(@PathVariable Long id) {
        return postUserService.look(id);
    }

    /**
     * 分页查询历史文章id列表
     *
     * @param pageRequest
     * @return
     */
    @ApiOperation("查询历史文章id列表")
    @PostMapping("/ids/look")
    @PreAuthorize("hasAuthority('post:user:query:look')")
    public ResponseResult<Page<Long>> queryPostIdByLook(@RequestBody PageRequest pageRequest) {
        return postUserService.findPostIdListByLook(pageRequest);
    }

    /**
     * 分页查询收藏文章id列表
     *
     * @param pageRequest
     * @return
     */
    @ApiOperation("查询收藏文章id列表")
    @PostMapping("/ids/collect")
    @PreAuthorize("hasAuthority('post:user:query:collect')")
    public ResponseResult<Page<Long>> queryPostIdByCollect(@RequestBody PageRequest pageRequest) {
        return postUserService.findPostIdListByCollect(pageRequest);
    }

    @ApiOperation("获取文章数量信息")
    @GetMapping("/num/{key}/{id}")
    public ResponseResult<Integer> queryNum(@PathVariable Long id, @PathVariable String key) {
        return postUserService.getNum(key, id);
    }

    @ApiOperation("获取文章数量信息-多")
    @GetMapping("/allnum/{id}")
    public ResponseResult<Map<String,Integer>> queryNum(@PathVariable Long id) {
        return postUserService.getAllNum(id);
    }
}
